<?php

namespace App\Models\Article;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;

class ArticleRecommendModel extends BaseModel
{
    use HasFactory, KfUid, KfSoftDeletes;

    protected $fillable = [
        'id',
        'type',
        'articleId',
        'title',
        'startAt',
        'endAt',
        'description',
        'cover',
        'sort',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'createdAt',
        'updatedByUid',
        'isDeleted',
        "contentType",
        "effectiveType",
        'redirectUrl',
    ];

    protected $table = "article_recommend";

    const CONTENT_TYPE_ARTICLE      = 1;
    const CONTENT_TYPE_CATEGORY     = 2;
    const CONTENT_TYPE_REDIRECT_URL = 3;
    const CONTENT_TYPE_TEXT         = [
        self::CONTENT_TYPE_ARTICLE      => "文章",
        self::CONTENT_TYPE_CATEGORY     => "专题",
        self::CONTENT_TYPE_REDIRECT_URL => '链接',
    ];

    const EFFECTIVE_TYPE_LIMIT  = 1;
    const EFFECTIVE_TYPE_ALWAYS = 2;
    const EFFECTIVE_TYPE_TEXT   = [
        self::EFFECTIVE_TYPE_LIMIT  => "限时生效",
        self::EFFECTIVE_TYPE_ALWAYS => "永久有效",
    ];

    const TYPE_NORMAL  = 0;
    const TYPE_SUBJECT = 1;
    const TYPE_NEW     = 2;
    const TYPE_TEXT    = [
        self::TYPE_NORMAL  => "黄金展示位",
        self::TYPE_SUBJECT => "专题展示位",
        self::TYPE_NEW     => "最新展示位",
    ];

    const TYPE_ENG_TEXT = [
        self::TYPE_NORMAL  => "homeRecommend",
        self::TYPE_SUBJECT => "subjectRecommend",
        self::TYPE_NEW     => "newRecommend",
    ];

    const TYPE_MIN_COUNT = [
        self::TYPE_ENG_TEXT[self::TYPE_NORMAL]  => 1,
        self::TYPE_ENG_TEXT[self::TYPE_SUBJECT] => 2,
    ];

    const STATUS_ON   = 1;
    const STATUS_OFF  = 2;
    const STATUS_WAIT = 3;

    const STATUS_TEXT = [
        self::STATUS_ON   => "已生效",
        self::STATUS_OFF  => "已过期",
        self::STATUS_WAIT => "未生效",
    ];

    const ACTIVATE_ON  = 1;
    const ACTIVATE_OFF = 0;

    const ACTIVATE_TEXT = [
        self::ACTIVATE_ON  => '上架',
        self::ACTIVATE_OFF => '下架',
    ];


    public function articleRel(): HasOne
    {
        return $this->hasOne(ArticleModel::class, "id", "articleId");
    }

    public function getStatusTextAttribute()
    {
        return self::STATUS_TEXT[$this->getStatusAttribute()];
    }

    /**
     * @return int
     */
    public function getStatusAttribute(): int
    {
        switch ($this->effectiveType) {
            case self::EFFECTIVE_TYPE_LIMIT:
                if (time() > $this->startAt) {
                    if (time() > $this->endAt) {
                        $status = self::STATUS_OFF;
                    } else {
                        $status = self::STATUS_ON;
                    }
                } else {
                    $status = self::STATUS_WAIT;
                }
                break;
            case self::EFFECTIVE_TYPE_ALWAYS:
                $status = self::STATUS_ON;
                break;
        }
        return $status;
    }
}
